package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.PayAdapter;
import com.hemaapp.yjnh.bean.Pay;
import com.hemaapp.yjnh.bean.User;

/**
 * 充值记录
 * */
public class ChargeListActivity extends BaseActivity {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private RefreshLoadmoreLayout layoutRefresh;
	private XtomListView listview;
	private LinearLayout ll_return_top;

	private PayAdapter payAdapter;
	private ArrayList<Pay> pays = new ArrayList<>();
	private int page = 0;
	private User user;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cmn_refresh_list_title);
		user = getApplicationContext().getUser();
		super.onCreate(savedInstanceState);
        getNetWorker().payList(user.getToken(), page+"");
	}

    @Override
    protected void getExras() {
    }

    @Override
    protected void findView() {
        titleText = (TextView) findViewById(R.id.title_text);
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleRight = (Button) findViewById(R.id.title_right_btn);

        layoutRefresh = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
        listview = (XtomListView) findViewById(R.id.listview);
        ll_return_top = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
    }

    @Override
    protected void setListener() {
        titleText.setText("充值记录");
        titleLeft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleRight.setVisibility(View.GONE);
        layoutRefresh.setOnStartListener(new OnStartListener() {
            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                page = 0;
                getNetWorker().payList(user.getToken(), page+"");
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                page++;
                getNetWorker().payList(user.getToken(), page+"");
            }
        });
        ll_return_top.setVisibility(View.GONE);
        listview.setDividerHeight(0);
    }

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PAY_LIST:
            showProgressDialog("请稍候");
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PAY_LIST:
            cancelProgressDialog();
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PAY_LIST:
			String page = netTask.getParams().get("page");
			HemaPageArrayResult<Pay> pResult = (HemaPageArrayResult<Pay>) baseResult;
			if ("0".equals(page)) {// 刷新
                layoutRefresh.refreshSuccess();
                layoutRefresh.setLoadmoreable(true);
				this.pays.clear();
				this.pays.addAll(pResult.getObjects());
			} else {// 更多
                layoutRefresh.loadmoreSuccess();
				if (pays.size() > 0)
					this.pays.addAll(pResult.getObjects());
				else {
                    layoutRefresh.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			freshData();
			break;
		default:
			break;
		}
	}

	private void freshData() {
		if (payAdapter == null) {
			payAdapter = new PayAdapter(mContext, pays);
			payAdapter.setEmptyString("暂无充值记录");
			listview.setAdapter(payAdapter);
		} else {
			payAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PAY_LIST:
			String page = netTask.getParams().get("page");
			if ("0".equals(page)) {// 刷新
				layoutRefresh.refreshFailed();
				freshData();
			} else {// 更多
                layoutRefresh.loadmoreFailed();
			}
            XtomToastUtil.showShortToast(mContext, "获取数据失败,"+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case PAY_LIST:
            String page = netTask.getParams().get("page");
            if ("0".equals(page)) {// 刷新
                layoutRefresh.refreshFailed();
                freshData();
            } else {// 更多
                layoutRefresh.loadmoreFailed();
            }
            XtomToastUtil.showShortToast(mContext, "获取数据失败,请稍后再试");
			break;
		default:
			break;
		}
	}
}
